﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPages_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PreRender += new EventHandler(InitPage);
        }
    }

    protected void InitPage(object sender, EventArgs e)
    {
        InitData();
    }

    protected bool IsOrdersInProcess()
    {
        //// Check if any orders are in process
        //String sqlCheckOrdersInProcess = "SELECT * FROM orders where status=2";
        //DataSet ds_checkorderinprocess = DB.QuerySelect(sqlCheckOrdersInProcess);

        //if (ds_checkorderinprocess.Tables[0].Rows.Count == 0)
        //{
        //    return false;
        //}

        return true;
    }

    protected void InitData()
    {
        //// Check if any orders are in process
        //if (!IsOrdersInProcess())
        //{
        //    // If no, start processing a new batch of the latest orders
        //    String sqlStartProcessing = "UPDATE orders SET status=2 WHERE status=1";
        //    DB.QueryAlter(sqlStartProcessing);
        //}

        //String sqlLuckyRestaurant = "SELECT restaurant.id as restaurant_id,restaurant.name as provider,phone,address " +
        //                                "FROM restaurant,owner " +
        //                                "WHERE restaurant.owner_id=owner.id and restaurant.id in " +
        //                                "(select restaurant_id from CategoryDish,orders_category_dish,orders " +
        //                                "where orders.status =2 and orders.id=orders_category_dish.orders_id " +
        //                                "and orders_category_dish.categorydish_id=CategoryDish.id)";

        //DataSet dsLuckyRestaurant = DB.QuerySelect(sqlLuckyRestaurant);

        //luckyrestaurant_datagrid.DataSource = dsLuckyRestaurant;
        //luckyrestaurant_datagrid.DataBind();
    }

    // 0 created
    // 1 summitted
    // 2 start processing
    // 3 sending to restaurant completed
    // 4 dispatching to deliver completed
    // 5 order handling finished
    protected void OnClickDetailBtn(object sender, DataGridCommandEventArgs e)
    {
        //String restaurant_id = luckyrestaurant_datagrid.DataKeys[e.Item.ItemIndex].ToString();
        //String restaurant_name = luckyrestaurant_datagrid.Items[e.Item.ItemIndex].Cells[1].Text;

        //// Get it's pay_rate
        //String sqlBusinessRate = "SELECT business_rate from restaurant where id="+restaurant_id;
        //DataSet ds_businessrate = DB.QuerySelect(sqlBusinessRate);
        //float business_rate = float.Parse(ds_businessrate.Tables[0].Rows[0].ItemArray[0].ToString());

        //Button dbtn = (Button)luckyrestaurant_datagrid.Items[e.Item.ItemIndex].FindControl("detail_button");
        //dbtn.Text = "再次显示";

        //// Get it's all order infos and send manually
        //String sqlOrderList = "SELECT dish.name as dishname,CONVERT(varchar(12), price, 1) as price,num," +
        //                      "CONVERT(varchar(12), price*num, 1) as shouldpay "+
        //                      "FROM orders,orders_category_dish,CategoryDish,dish "+
        //                      "WHERE orders.status=2 and orders_category_dish.orders_id=orders.id and "+
        //                      "CategoryDish.restaurant_id="+restaurant_id+" and "+
        //                      "orders_category_dish.categorydish_id=CategoryDish.id and CategoryDish.dish_id=dish.id "+
        //                      "ORDER BY orders.id";
        //DataSet ds_orderlist = DB.QuerySelect(sqlOrderList);

        //int num = 0;
        //float sum = 0;
        //float sum_rate = 0;
        //String info = restaurant_name + "\n" + DateTime.Now.ToString() + "\n";
        //info += "菜名-价-数量\n"; 
        //for (int count = 0; count < ds_orderlist.Tables[0].Rows.Count; count++)
        //{
        //    info += ds_orderlist.Tables[0].Rows[count].ItemArray[0].ToString()+" ";
        //    info += ds_orderlist.Tables[0].Rows[count].ItemArray[1].ToString() + " ";
        //    info += ds_orderlist.Tables[0].Rows[count].ItemArray[2].ToString() + "份 ";
        //    info += ds_orderlist.Tables[0].Rows[count].ItemArray[3].ToString() + "元\n";

        //    num += int.Parse(ds_orderlist.Tables[0].Rows[count].ItemArray[2].ToString());
        //    sum += float.Parse(ds_orderlist.Tables[0].Rows[count].ItemArray[3].ToString());
        //}
        //sum_rate = sum * business_rate / 100;
        //info += "总计:" + num + "份  " + sum + "元*"+business_rate+"%="+sum_rate+ "元\n";
        //info += "有问题 请及时反馈.谢谢";

        //details_textbox.Text = info;
    }

    protected void OnClickFinishBatchBtn(object sender, EventArgs e)
    {
        //// Check if all luck restaurant are handled by checking btn text.
        //for (int i = 0; i < luckyrestaurant_datagrid.Items.Count; i++)
        //{
        //    Button dbtn = (Button)luckyrestaurant_datagrid.Items[i].FindControl("detail_button");
        //    if (dbtn.Text.Equals("显示"))
        //    {
        //        notice_done_label.Text="还有未发送的餐厅订单.";
        //        notice_done_label.ForeColor = System.Drawing.Color.Red;
        //        return;
        //    }
        //}

        //// Change the orders status to 3 
        //String sqlFinishBatch = "UPDATE orders SET status=3 WHERE status=2";
        //DB.QueryAlter(sqlFinishBatch);

        //notice_done_label.Text = "完成,稍后可做分派快递的操作!";
        //notice_done_label.ForeColor = System.Drawing.Color.Blue;

        //finishbatch_button.Enabled = false;
    }
}
